using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class forums_controls_search_searchresults : Controls.BaseControl
{
    Regex removeYCodes = new Regex(@"\[quote.*?\].*?\[/quote\]", RegexOptions.Singleline | RegexOptions.Compiled);

    Helpers.ForumSearchHelper search;
    string querystring;

    private int CurrentPage
    {
        get
        {
            int page;
            if (Helpers.QueryStringHelper.TryGetInt32("page", out page))
            {
                return page;
            }
            else
            {
                return 1;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        querystring = HttpContext.Current.Request.ServerVariables["Query_String"];
        querystring = Regex.Replace(querystring, @"&?page=\d*", "");
        search = Helpers.ForumSearchHelper.DecodeFromQuerystring(querystring);
        int totalResults = GetTotalResults();
        if (totalResults <= 0)
        {
            lblResults.Visible = false;
            mviewResults.SetActiveView(viewOhNoes);
        }
        else
        {
            lblResults.Visible = true;
            lblResults.Text = string.Format("{0} records found", totalResults);
            Refresh();
        }
        Page.Title = "Search Results";
    }

    private int GetTotalResults()
    {
        int total = 0;
        if (search != null)
        {
            SqlCommand comm = search.GenerateSearchCountSqlCommand();
            comm.Connection = DAL.DataManagerConnection.Connection;
            SqlDataAdapter adapter = new SqlDataAdapter(comm);
            DataTable table = new DataTable();
            adapter.Fill(table);
            if (Int32.TryParse(table.Rows[0][0].ToString(), out total))
            {
                Pager1.TotalPages = (int)Math.Ceiling(total / 25.0);
                Pager1.CurrentPage = CurrentPage;
                Pager1.UrlFormat = "~/forums/searchresults.aspx?" + querystring + "&page={0}";
            }
            else
            {
                Pager1.TotalPages = 0;
                total = 0;
            }
            comm.Connection.Close();
        }

        return total;
    }



    private void Refresh()
    {
        if (search != null)
        {
            switch (search.DisplayMode)
            {
                case Helpers.ForumSearchHelper.Display.AsThreads:
                    List<DAL.Thread> threads = DAL.Threads.Search(search, CurrentPage);
                    gridThreads.DataSource = threads;
                    gridThreads.DataBind();
                    mviewResults.SetActiveView(viewThreadMode);
                    break;
                case Helpers.ForumSearchHelper.Display.AsPosts:
                    List<DAL.Post> posts = DAL.Posts.Search(search, CurrentPage);
                    rptPosts.DataSource = posts;
                    rptPosts.DataBind();
                    mviewResults.SetActiveView(viewPostMode);
                    break;
            }
        }
        else
        {
            mviewResults.SetActiveView(viewOhNoes);
        }
    }

    protected string GetPostText(string value)
    {
        string originalValue = value;
        bool truncated = false;
        value = removeYCodes.Replace(value, "");

        if (value.Length == 0)
        {
            value = originalValue;
        }
        if (value.Length > 500)
        {
            truncated = true;
            value = value.Substring(0, 500);
        }

        value = Helpers.StringHelper.SafeFormat(value).Trim();

        string[] lines = value.Split("\n".ToCharArray(), 11);
        if (lines.Length > 10)
        {
            truncated = true;
            value = string.Join("<br />", lines, 0, 10);
        }
        else
        {
            value = string.Join("<br />", lines);
        }

        if (truncated)
        {
            return value + " [...]";
        }
        else
        {
            return value;
        }
    }
}
